using System.Linq;
using Headspring.Training.Core.Model;
using Headspring.Training.Core.Services;
using NHibernate;
using NHibernate.Expression;

namespace Headspring.Training.Infrastructure.Impl
{
	public class EmployeeRepository : RepositoryBase, IEmployeeRepository
	{
		public EmployeeRepository(ISessionBuilder sessionFactory)
			: base(sessionFactory)
		{
		}

		public Employee GetByUserName(string userName)
		{
			ISession session = getSession();
			ICriteria criteria = session.CreateCriteria(typeof (Employee));
			criteria.Add(Expression.Eq("UserName", userName));

			return criteria.UniqueResult<Employee>();
		}

		public Employee[] GetAllEmployeesSorted()
		{
			ISession session = getSession();
			ICriteria criteria = session.CreateCriteria(typeof (Employee));
			criteria.AddOrder(Order.Asc("LastName")).AddOrder(Order.Asc("FirstName"));

			return criteria.List<Employee>().ToArray();
		}
	}
}